package wpRelations;

import java.io.PrintWriter;
import java.util.ArrayList;

import processing.core.PApplet;

public class TsvWriterModule {
	PrintWriter output;
	//String title="Coby_Miller";
	//boolean isTitleAPerson;
	public static final int ARTICLE = 0;
	public static final int PERSON = 1;
	public static final int NON_PERSON = 2;
	public static final int CATEGORY = 3;
	PApplet parent;
	boolean checkPerson = false;
	
	public TsvWriterModule (PApplet parent) {
		this.parent = parent;
	}
	
	public void init(String theOutputFileName) {
		//output = createWriter(title + "_links.tsv");
		output = parent.createWriter(theOutputFileName);
	}
	
	public void setCheckPerson() {
		checkPerson = true;
	}
	
	public void unSetCheckPerson() {
		checkPerson = false;
	}
	
	public void printStringRow(String[] s) {
		for (int i = 0; i < s.length-1; i++) {
			output.print(s[i] + "\t");
		}
		output.println(s[s.length-1]);
	}
	
	public void printLinks(String fromTitle) {
		  wArticle fromArticle = new wArticle(parent, fromTitle);
		  boolean isTitleAPerson = false;
		  if (checkPerson) isTitleAPerson = fromArticle.checkPerson();
		  ArrayList<wArticle> links = fromArticle.getLinks();
		  for (int i = 0; i < links.size(); i++) {
				System.out.println("writing link " + i + " out of " + links.size());
				String in = fromArticle.title;
				String out = links.get(i).title;
				String inType;
				if (!checkPerson) {
					inType = "article";
				} else {
				if (isTitleAPerson) {
					inType = "person";
				} else {
					inType = "non_person";
				}
				}
				String outType;
				boolean isAPerson = false;
				if(checkPerson) isAPerson = links.get(i).checkPerson();
				if (!checkPerson) {
					outType = "article";
				} else {
				if (isAPerson) {
					outType = "person";
				} else {
					outType = "non_person";
				}
				}
				output.println(in + "\t" + out + "\t" + inType + "\t" + outType);
			  }
	}
	
	public  void printBackLinks (String fromTitle) {
		  wArticle fromArticle = new wArticle(parent, fromTitle);
		  ArrayList<wArticle> backLinks = fromArticle.getBackLinks();
		  boolean isTitleAPerson = false;
		  if (checkPerson) isTitleAPerson = fromArticle.checkPerson();
		  //// writes all backlinks to title to file
		  for (int i = 0; i < backLinks.size(); i++) {
			System.out.println("writing backLink " + i + " out of " + backLinks.size());
			String in =backLinks.get(i).title; 
			String out = fromArticle.title;
			String inType;
			boolean isAPerson = false;
			if(checkPerson) isAPerson = backLinks.get(i).checkPerson();
			if (!checkPerson) {
				inType = "article";
			} else {
			if (isAPerson) {
				inType = "person";
			} else {
				inType = "non_person";
			}
			}
			String outType;
			if (!checkPerson) {
				outType = "article";
			} else {
			if (isTitleAPerson) {
				outType = "person";
			} else {
				outType = "non_person";
			}
			}
			output.println(in + "\t" + out + "\t" + inType + "\t" + outType);
		  }

	}
	
	public void printSuperCategories (String fromTitle) {
		  wArticle fromArticle = new wArticle(parent, fromTitle);
		  ArrayList<wCategory> categories = fromArticle.getCategories();
		  boolean isTitleAPerson = false;
		  if (checkPerson) isTitleAPerson = fromArticle.checkPerson();
		  //// writes all categories containing article_zero to file
		  for (int i = 0; i < categories.size(); i++) {
			System.out.println("writing category " + i + " out of " + categories.size());
			String in =categories.get(i).title; 
			String out = fromArticle.title;
			String inType = "category";
			String outType;
			if (!checkPerson) {
				outType = "article";
			} else {
			if (isTitleAPerson) {
				outType = "person";
			} else {
				outType = "non_person";
			}
			}
			output.println(in + "\t" + out + "\t" + inType + "\t" + outType);
		  }	  
	}
	
	public void printSubCategoriesPages(String fromCategoryTitle) {
		wCategory fromCategory = new wCategory(parent, fromCategoryTitle);
		fromCategory.setSubCatsPages();
		for (int j = 0; j < fromCategory.subCats.size(); j++) {
			System.out.println("writing subCategory " + j + " out of " + fromCategory.subCats.size());
			String in =fromCategory.title; 
			String out = fromCategory.subCats.get(j).title;
			String inType = "category";
			String outType = "category";
			output.println(in + "\t" + out + "\t" + inType + "\t" + outType);
		}
	
		  for (int j = 0; j < fromCategory.getSubPages().size(); j++) {
				System.out.println("writing page " + j + " out of " + fromCategory.pages.size());
				String in =fromCategory.title; 
				String out = fromCategory.pages.get(j).title;
				String inType = "category";
				String outType;
				boolean isAPerson = false;
				if(checkPerson) isAPerson = fromCategory.pages.get(j).checkPerson();
				if (!checkPerson) {
					outType = "article";
				} else {

				if (isAPerson) {
					outType = "person";
				} else {
					outType = "non_person";
				}
				}
				output.println(in + "\t" + out + "\t" + inType + "\t" + outType);
			  }

	}
	
	public void finish() {
	    output.flush(); // Write the remaining data
	    output.close(); // Finish the file
	    System.out.println("finished.");	
	}
		
}
